﻿using Exam.Repository.Interface;
using Exam.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace Exam.Service.EFCore.MySql
{
    public class ExmaEFCoreService<T> : IExamEFCoreService<T> where T : class, new()
    {
        private readonly IRepository<T> _repository;

        public ExmaEFCoreService(IRepository<T> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        public void Add(T t)
        {
            _repository.Add(t);
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="ie"></param>
        public void AddRange(IEnumerable<T> ie)
        {
            _repository.AddRange(ie);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        public void Delete(T t)
        {
            _repository.Delete(t);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ie"></param>
        public void DeleteRange(IEnumerable<T> ie)
        {
            _repository.DeleteRange(ie);
        }
        /// <summary>
        /// 添加查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> QueryAll(Expression<Func<T, bool>>? where)
        {
            return _repository.QueryAll(where);
        }
        /// <summary>
        /// 排序查询
        /// </summary>
        /// <typeparam name="type"></typeparam>
        /// <param name="order"></param>
        /// <param name="IsAsc"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool IsAsc = true, Expression<Func<T, bool>>? where = null)
        {
            return _repository.QueryAll(order, IsAsc, where);
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="type"></typeparam>
        /// <param name="total"></param>
        /// <param name="skip"></param>
        /// <param name="take"></param>
        /// <param name="order"></param>
        /// <param name="IsAsc"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> QueryAll<type>(out int total, int page, int limit, Expression<Func<T, type>> order, bool IsAsc = true, Expression<Func<T, bool>>? where = null)
        {
            return _repository.QueryAll(out total, (page - 1) * limit, limit, order, IsAsc, where);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public int SaveChanges()
        {
            return _repository.SaveChanges();
        }
        public async Task<int> SaveChangesAsync()
        {
            return await _repository.SaveChangesAsync();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        public void Update(T t)
        {
            _repository.Update(t);
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="ie"></param>
        public void UpdateRange(IEnumerable<T> ie)
        {
            _repository.UpdateRange(ie);
        }
        /// <summary>
        /// 事务
        /// </summary>
        /// <typeparam name="type"></typeparam>
        /// <typeparam name="type2"></typeparam>
        /// <param name="ireaction"></param>
        /// <param name="ireaction2"></param>
        /// <returns></returns>
        public bool Transactions<type, type2>(IExamEFCoreService<type> ireaction, IExamEFCoreService<type2> ireaction2)
            where type : class, new()
            where type2 : class, new()
        {
            using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))
                try
                {
                    ireaction.SaveChanges();
                    ireaction2.SaveChanges();
                    scope.Complete();
                    return true;

                }
                catch (Exception e)
                {
                    return false;
                }
                finally
                {

                }
        }

        public Task<IQueryable<T>> QueryAllAsync(Expression<Func<T, bool>>? where)
        {
            return _repository.QueryAllAsync(where);
        }

        public Task<IQueryable<T>> QueryAllAsync<type>(Expression<Func<T, type>> order, bool IsAsc = true, Expression<Func<T, bool>>? where = null)
        {
            return _repository.QueryAllAsync(order, IsAsc, where);
        }

        public Task<IQueryable<T>> QueryAllAsync<type>(int page, int limit, Expression<Func<T, type>> order, bool IsAsc = true, Expression<Func<T, bool>>? where = null)
        {
            return _repository.QueryAllAsync((page - 1) * limit, limit, order, IsAsc, where);
        }

       
    }
}
